home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VGATUT2.ZIP / CIRCFUNC.CPP next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  2.5 KB  |  106 lines

  1. //------------------------------------------------------------
  2. // Circle Routines - Barny Mercer 3/8/95 @ 11:45pm
  3. //------------------------------------------------------------
  4.  
  5. #include "vgafunc.h"
  6. #include <memory.h>
  7. #include <math.h>
  8. #include <conio.h>
  9.  
  10. // this is the same as the 'vga' variable in vgafunc.cpp
  11. // but is renamed for this function only
  12.  
  13. unsigned char *screen= (unsigned char *)0xA0000000;
  14.  
  15. //------------------------------------------------------------
  16. // DrawCircle -  Pretty useless routine which illustrates the
  17. //         fundamentals of circle drawing
  18. //------------------------------------------------------------
  19.  
  20. void DrawCircle( int X, int Y, int Radius, unsigned char Colour )
  21. {
  22.     float xt, yt;
  23.     float rt;
  24.  
  25.     // begin with offset = 0 degrees
  26.  
  27.     for (float Tmp=0; Tmp<6.3; Tmp += 0.01)
  28.     {
  29.     xt = (float)Radius * cos(Tmp) + X;
  30.     yt = (float)Radius * sin(Tmp) + Y;
  31.  
  32.     PutPixel( (int)xt, (int)yt, Colour );
  33.     }
  34.  
  35. }
  36. //------------------------------------------------------------
  37. // ImpDrawCircle - A much better routine which is based on the
  38. //           above function but uses a couple of methods
  39. //           to increase the speed & accuracy.
  40. //------------------------------------------------------------
  41.  
  42. void ImpDrawCircle( int X, int Y, int Radius, unsigned char Colour )
  43. {
  44.     float xt, yt;
  45.     float rt, increment;
  46.  
  47.     int NewX, NewY;
  48.  
  49.     int NewX1, NewY1;
  50.     int NewX2, NewY2;
  51.     int NewX3, NewY3;
  52.     int NewX4, NewY4;
  53.  
  54.     if (Radius <= 0)
  55.     Radius = 1;
  56.  
  57.     increment = 1/(float)Radius;
  58.  
  59.     // calculate X, Y change for each segment based on radius
  60.  
  61.     for (float Tmp=0; Tmp<1.6; Tmp += increment)
  62.     {
  63.     // positioning no longer happens here
  64.     xt = (float)Radius * (float)cos(Tmp);
  65.     yt = (float)Radius * (float)sin(Tmp);
  66.  
  67.     if ( abs( (long)(xt - (int)xt)) < 0.5 )
  68.     {
  69.         if (xt > 0)
  70.         NewX = (int)(xt+0.5);
  71.         else
  72.         NewX = (int)(xt-0.5);
  73.     }
  74.     else
  75.         NewX = (int)(xt);
  76.  
  77.     if ( abs( (long)(yt - (int)yt)) < 0.5 )
  78.     {
  79.         if (yt > 0)
  80.         NewY = (int)(yt+0.5);
  81.         else
  82.         NewY = (int)(yt-0.5);
  83.     }
  84.     else
  85.         NewY = (int)(yt);
  86.  
  87.     NewX1 = NewX + X;
  88.     NewY1 = NewY + Y;
  89.  
  90.     NewX2 = (NewX * -1) + X;
  91.     NewY2 = (NewY * -1) + Y;
  92.  
  93.     NewX3 = (NewX * -1) + X;
  94.     NewY3 =  NewY + Y;
  95.  
  96.     NewX4 =  NewX + X;
  97.     NewY4 = (NewY * -1) + Y;
  98.  
  99.     screen[( (NewY1<<8) + (NewY1<<6)+NewX1 )] = Colour;
  100.     screen[( (NewY2<<8) + (NewY2<<6)+NewX2 )] = Colour;
  101.     screen[( (NewY3<<8) + (NewY3<<6)+NewX3 )] = Colour;
  102.     screen[( (NewY4<<8) + (NewY4<<6)+NewX4 )] = Colour;
  103.     }
  104.  
  105. }
  106.